package com.atguign.gulimall.coupon.service.impl;

import com.atguign.gulimall.coupon.entity.MemberPriceEntity;
import com.atguign.gulimall.coupon.entity.SkuLadderEntity;
import com.atguign.gulimall.coupon.service.MemberPriceService;
import com.atguign.gulimall.coupon.service.SkuLadderService;
import com.atguigu.common.to.MemberPriceTo;
import com.atguigu.common.to.SkuReductionTo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.Query;

import com.atguign.gulimall.coupon.dao.SkuFullReductionDao;
import com.atguign.gulimall.coupon.entity.SkuFullReductionEntity;
import com.atguign.gulimall.coupon.service.SkuFullReductionService;
import org.springframework.transaction.annotation.Transactional;


@Service("skuFullReductionService")
public class SkuFullReductionServiceImpl extends ServiceImpl<SkuFullReductionDao, SkuFullReductionEntity> implements SkuFullReductionService {
    @Autowired
    private SkuLadderService skuLadderService;

    @Autowired
    private MemberPriceService memberPriceService;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SkuFullReductionEntity> page = this.page(
                new Query<SkuFullReductionEntity>().getPage(params),
                new QueryWrapper<SkuFullReductionEntity>()
        );

        return new PageUtils(page);
    }

    /**
     * 保存sdk优惠
     * @param skuReductionTo
     */
    @Transactional
    @Override
    public void saveSkuReduction(SkuReductionTo skuReductionTo) {
        //满件数优惠
        if(skuReductionTo.getFullCount()>0){
            SkuLadderEntity skuLadderEntity = new SkuLadderEntity();
            BeanUtils.copyProperties(skuReductionTo,skuLadderEntity);
            skuLadderEntity.setAddOther(skuReductionTo.getCountStatus());
            skuLadderService.save(skuLadderEntity);
        }
        //满金额优惠
        if(skuReductionTo.getFullPrice().compareTo(BigDecimal.valueOf(0))>0){
            SkuFullReductionEntity skuFullReductionEntity = new SkuFullReductionEntity();
            BeanUtils.copyProperties(skuReductionTo,skuFullReductionEntity);
            skuFullReductionEntity.setAddOther(skuReductionTo.getPriceStatus());
            this.save(skuFullReductionEntity);
        }


        List<MemberPriceTo> memberPrice = skuReductionTo.getMemberPrice();
        if(memberPrice!=null&&!memberPrice.isEmpty()){
            List<MemberPriceEntity> collect = memberPrice.stream().map(memberPriceTo -> {
                MemberPriceEntity memberPriceEntity = new MemberPriceEntity();
                memberPriceEntity.setSkuId(skuReductionTo.getSkuId());
                memberPriceEntity.setMemberLevelId(memberPriceTo.getId());
                memberPriceEntity.setMemberLevelName(memberPriceTo.getName());
                memberPriceEntity.setMemberPrice(memberPriceTo.getPrice());
                memberPriceEntity.setAddOther(1);
                return memberPriceEntity;
            }).filter(item->item.getMemberPrice()!=null&&item.getMemberPrice().compareTo(BigDecimal.valueOf(0))!=0).collect(Collectors.toList());
            memberPriceService.saveBatch(collect);
        }
    }

}